package com.klhappy.houyuan.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.klhappy.houyuan.bean.HouYuanBean.GoodsRecord;
import com.klhappy.houyuan.bean.MoreEvalustion.PageInfor;
import com.klhappy.houyuan.utils.library.TagBaseAdapter;
import com.klhappy.houyuan.utils.library.TagCloudLayout;
import com.google.gson.Gson;
import com.klhappy.houyuan.R;
import com.klhappy.houyuan.adapter.LookMoreAdapter;
import com.klhappy.houyuan.bean.BaseJsonBean;
import com.klhappy.houyuan.bean.MoreEvalustion.Comment;
import com.klhappy.houyuan.bean.MoreEvalustion.MoreEvaBean;
import com.klhappy.houyuan.bean.MoreEvalustion.Records;
import com.klhappy.houyuan.constant.UrlConstant;
import com.klhappy.houyuan.dialog.NetWorkDialog;
import com.klhappy.houyuan.interfaces.CallBackInterModle;
import com.klhappy.houyuan.modle.HouYuanModle;
import com.klhappy.houyuan.utils.JsonCode;
import com.klhappy.houyuan.view.AbsActivity;

public class MoreEvaluationActivity extends AbsActivity implements CallBackInterModle {
    @InjectView(R.id.title)
    TextView title;
    @InjectView(R.id.tv_ratingRation)
    TextView tv_ratingRation;
    @InjectView(R.id.tv_RatingCount)
    TextView tv_RatingCount;
    @InjectView(R.id.container)
    TagCloudLayout container;
    @InjectView(R.id.lv_evalation)
    PullToRefreshListView lv_evalation;

    @OnClick(R.id.click_ra)
    public void click_ra() {
        finish();
    }

    private TagBaseAdapter mAdapter;
    private List<String> mList;
    private LookMoreAdapter adapter;
    private String g_id;
    private HouYuanModle modle;
    private MoreEvaBean evaBean;
    private PageInfor infor;
    private Comment comment;
    private List<Records> nowRecords;
    private List<Records> newRecord;
    private NetWorkDialog dialog;
    private String downPage = "";
    private boolean canLoad = false;
    private String path;
    private boolean flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.more_evaluation);
        ButterKnife.inject(this);
        title.setText("更多评价");

        InModle();
    }

    private void InModle() {
        g_id = getIntent().getStringExtra("g_id");
        modle = new HouYuanModle(this);
        modle.setCallBackInterModle(this);
        modle.getComments(g_id);
        showDialog("请稍等...");
        dialog = new NetWorkDialog(this);

        lv_evalation.setMode(PullToRefreshBase.Mode.BOTH);
        lv_evalation.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                path = UrlConstant.BASE_URL + infor.getTheFirst_url();
                flag = true;
                modle.getNewComments(g_id, path);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                if (infor.getDownPage_url() != null && !infor.getDownPage_url().equals("")) {
                    path = UrlConstant.BASE_URL + infor.getDownPage_url();
                    flag = false;
                    modle.getNewComments(g_id, path);
                } else {
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            showToastx("亲,已全部加载完!");
                            lv_evalation.onRefreshComplete();
                        }
                    }, 500);
                }
            }
        });
    }

    private void setGoodsList(List<Records> list,boolean refresh){
        if (refresh) {
            nowRecords.clear();
        }
        nowRecords.addAll(list);
        newRecord.clear();
        newRecord.addAll(nowRecords);
        adapter.setNotif(newRecord);
    }

    @Override
    public void CallBackFromModle(String url, BaseJsonBean o) {
        if (url.equals(UrlConstant.COMMENTS_URL)) {
            if (o.getCode() == 200) {
                evaBean = JsonCode.getClass(o.getEntity(), MoreEvaBean.class);
                comment = JsonCode.getClass(evaBean.getComment(), Comment.class);
                nowRecords = JsonCode.getClassList(evaBean.getRecords(), Records.class);
                infor = JsonCode.getClass(evaBean.getPageInfor(), PageInfor.class);
                try {
                    setView();
                } catch (Exception e) {

                }
            }else {
                dialog.netWork(o.getMsg());
                dialog.show();
            }
            closeDialog();
        }else if (url.equals(path)){
            evaBean = JsonCode.getClass(o.getEntity(), MoreEvaBean.class);
            comment = JsonCode.getClass(evaBean.getComment(), Comment.class);
            newRecord = JsonCode.getClassList(evaBean.getRecords(), Records.class);
            infor = JsonCode.getClass(evaBean.getPageInfor(), PageInfor.class);
            setGoodsList(newRecord, flag);
            lv_evalation.onRefreshComplete();
        }
    }

    private void setView() {
        tv_ratingRation.setText(comment.getPercent());
        tv_RatingCount.setText(comment.getReplyNo() + "已评价");
        adapter = new LookMoreAdapter(this, nowRecords);
        lv_evalation.setAdapter(adapter);

        mList = new ArrayList<>();
        Pattern p = Pattern.compile("[^0-9]");
        Matcher m = p.matcher(comment.getUmarks());
        String result = m.replaceAll("");
        for (int i = 0; i < result.length(); i++) {
            String num = result.substring(i, i + 1);
            if (num.equals("1")) {
                mList.add("非常新鲜");
            } else if (num.equals("2")) {
                mList.add("物流很快");
            } else if (num.equals("3")) {
                mList.add("服务很好");
            }
        }
        mAdapter = new TagBaseAdapter(this, mList);
        container.setAdapter(mAdapter);
    }
}
